package com.theme.passport.service.theme.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.utils.Query;
import com.theme.passport.dao.CustomMapper;
import com.theme.passport.dao.theme.UserBuyDao;
import com.theme.passport.entity.theme.UserBuyEntity;
import com.theme.passport.service.theme.UserBuyService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("userBuyService")
public class UserBuyServiceImpl extends ServiceImpl<UserBuyDao, UserBuyEntity> implements UserBuyService {

    @Autowired
    private CustomMapper mapper;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String userId = (String) params.get("userId");
        String type = (String) params.get("type");
        String objectId = (String) params.get("objectId");
        IPage<UserBuyEntity> page = this.page(
                new Query<UserBuyEntity>().getPage(params),
                new QueryWrapper<UserBuyEntity>()
                        .eq(StringUtils.isNotBlank(userId), "user_id", userId)
                        .eq(StringUtils.isNotBlank(type), "type", type)
                        .eq(StringUtils.isNotBlank(objectId), "object_id", objectId)
                        .orderByDesc("created")
        );
        List<UserBuyEntity> list = new ArrayList<>();
        list= page.getRecords();
        for (UserBuyEntity entity : list){
            if(entity.getType() == 1){
                entity.setXzl( mapper.getOneInteger("select count(id) from tab_download_history where object_id  = "+entity.getObjectId()+" and  type = 1"));
            }
        }
        return new PageUtils(page);
    }

    @Override
    public List<UserBuyEntity> queryList(Map<String, Object> params) {
        String userId = (String) params.get("userId");
        String type = (String) params.get("type");
        String objectId = (String) params.get("objectId");

        Wrapper<UserBuyEntity> queryWrapper = new QueryWrapper<UserBuyEntity>()
                .eq(StringUtils.isNotBlank(userId), "user_id", userId)
                .eq(StringUtils.isNotBlank(type), "type", type)
                .eq(StringUtils.isNotBlank(objectId), "object_id", objectId);

        List<UserBuyEntity> userBuyList = this.baseMapper.selectList(queryWrapper);

        return userBuyList;
    }

}